//
//  BNDOfficeDocSignedView.m
//  OfficeDoc
//
//  Created by pie on 2022/4/3.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocSignedView.h"
#import "BNDofficeDocSignedVc.h"
#import "BNDPushVCHandler.h"
#import "UIImage+YYAdd.h"
#import "UIImageView+LKWebImage.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>
#import "BndUser.h"
#import "BNDOfficeDocAlert.h"
#import "BNDOfficeDocHandle.h"
#import "LVHUD.h"


@interface BNDOfficeDocSignedView ()
@property (strong, nonatomic) UIButton *signedBtn;
@property (strong, nonatomic) UIButton *readBtn;
@property (strong, nonatomic) UIImageView *signedImageView;
@end

@implementation BNDOfficeDocSignedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _signedImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:UIColorWithHex(0xF6F7F9)]];
        _signedImageView.userInteractionEnabled = YES;
        [self addSubview:_signedImageView];
        _signedImageView.layer.cornerRadius = 6;
        _signedImageView.layer.masksToBounds = YES;
        _signedImageView.hidden = YES;
        [_signedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top);
            make.centerX.mas_equalTo(self);
            make.size.mas_equalTo(CGSizeMake(211, 73));
        }];
        
        
        _signedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_signedBtn lk_setImage:BNDImageNamed(@"bnd_tisi_icon")];
        [_signedBtn lk_setTitle:@"你已签署此公告"];
        [_signedBtn lk_setFont:BNDPFMediumFont(14)];
        [_signedBtn lk_setTextColor:UIColorWithHex(0x51555D)];
        _signedBtn.userInteractionEnabled = NO;
        _signedBtn.hidden = YES;
        _signedBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [self addSubview:_signedBtn];
        [_signedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.signedImageView.mas_bottom).offset(8);
            make.centerX.mas_equalTo(self);
            make.size.mas_equalTo(CGSizeMake(116, 20));
        }];
        
        _readBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_readBtn lk_setBackgroundImage:[UIImage imageWithColor:UIColorWithHex(0x3770EB)]];
        [_readBtn lk_setTitle:@"我已阅"];
        [_readBtn lk_setFont:BNDPFMediumFont(17)];
        [_readBtn lk_setTextColor:UIColorWithHex(0xffffff)];
        _readBtn.layer.cornerRadius = 8;
        _readBtn.layer.masksToBounds = YES;
        [self addSubview:_readBtn];
        [_readBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top);
            make.centerX.mas_equalTo(self);
            make.size.mas_equalTo(CGSizeMake(kDeviceWidth-70, 49));
        }];
        
        @weakify(self)
        [[self.readBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self handleSigned];
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
           @strongify(self)
            [self showImage];
        }];
        [_signedImageView addGestureRecognizer:tap];
    }
    
    return self;
}

- (void)handleSigned
{
    [LVHUD showLoding:[BNDPushVCHandler topViewController].view];
    @weakify(self)
    [BNDApiManager  officeDocRequestUserInfoVoByIds:^(NSArray<BndUser *> * _Nonnull moreClass) {
        @strongify(self)
        [LVHUD dismissLoding:[BNDPushVCHandler topViewController].view];
        if(moreClass.count>0){
            BndUser *userinfo = moreClass[0];
            [BNDPreference sharedInstance].userInfo.isCertificated = userinfo.isCertificated;
            [self handleJumpRealName];
        }
    } failure:^(NSError * _Nullable error) {
        [LVHUD dismissLoding:[BNDPushVCHandler topViewController].view];
        [ReminderHUD showReminderText:@"请求实名失败,请查看网络" delayTime:1];
    }];
}

- (void)handleJumpRealName
{
    @weakify(self)
    if([BNDPreference sharedInstance].userInfo == nil||([BNDPreference sharedInstance].userInfo.isCertificated == NO)){
        BNDOfficeDocAlert *customPopoverView = [[BNDOfficeDocAlert alloc] initWithTitle:@"您尚未实名认证，实名后才可进行签署操作" leftButtonTitle:@"暂不实名" rightButtonTitle:@"前往实名" clickAction:^(LKCustomButton clickNum) {
            @strongify(self)
             switch (clickNum) {
                 case LKCustomButtonRight: {
                     [self handleRealNameMethod];
                     break;
                 }
                 default:
                     break;
             }
         }];
         [customPopoverView show:[BNDPushVCHandler currentSelectedNavController].view];
        return;
    }
    
    [self handleJumpSignedVc];
}

/// 处理真人认证
- (void)handleRealNameMethod
{
    BND_BLOCK_EXEC([BNDPreference sharedInstance].realNameJumpBlock);
}

/// 处理签署页面
- (void)handleJumpSignedVc
{
    @weakify(self)
    [BNDofficeDocSignedVc bnd_sign:[BNDPushVCHandler topViewController] title:@"公告已阅签署" officeDocId:self.model.recordId comp:^(NSString * _Nullable imgstr) {
        @strongify(self)
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRefreshSuccess object:@"刷新列表"];
        [self handleSignedState:imgstr];
    } cancel:^{
        
    }];
}

/// 显示签署的图片
/// @param objectKey  图片链接
- (void)handleSignedState:(NSString *)objectKey
{
    _model.recordType = 3;
    NSString *imageUrl = [[BNDPreference sharedInstance] handleProduceImageUrl:objectKey];
    _model.signaturePic = imageUrl;
    _signedBtn.hidden = NO;
    _signedImageView.hidden = NO;
    _readBtn.hidden = YES;
    [_signedImageView lk_setImageFadeWithURLStr:imageUrl placeholder:[UIImage imageWithColor:UIColorWithHex(0xF6F7F9)]];
}

/// 查看图片
- (void)showImage
{
    NSMutableArray *photos = [NSMutableArray new];
    GKPhoto *photo = [GKPhoto new];
    photo.url = [NSURL URLWithString:self.model.signaturePic];
    [photos addObject:photo];
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:0];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    [browser showFromVC:[BNDPushVCHandler currentSelectedNavController]];
}

- (void)setModel:(BNDOfficeDocModel *)model
{
    _model = model;
    
    if(model.recordId.length ==0){
        _signedBtn.hidden = YES;
        _readBtn.hidden = YES;
        _signedImageView.hidden = YES;
        return;
    }
    
    if(model.needSigned == 0){
        _signedBtn.hidden = YES;
        _readBtn.hidden = YES;
        _signedImageView.hidden = YES;
    }else{
        if(model.recordType == 1){
            _signedBtn.hidden = YES;
            _signedImageView.hidden = YES;
            _readBtn.hidden = NO;
        }else if(model.recordType == 3){
            _signedBtn.hidden = NO;
            _signedImageView.hidden = NO;
            _readBtn.hidden = YES;
            [_signedImageView lk_setImageFadeWithURLStr:model.signaturePic placeholder:[UIImage imageWithColor:UIColorWithHex(0xF6F7F9)]];
        }else{
            _signedBtn.hidden = YES;
            _signedImageView.hidden = YES;
            _readBtn.hidden = NO;
        }
    }
}

@end
